﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ServiceApplication.Service.Basic;
using CommonLibrary.DataModel.Basic;
using System.Data;
using CommonLibrary.DataModel.WorkFlow;
using CommonLibrary.DataModel.Form;
using System.Xml.Linq;
using OAclient.Models;
using CommonLibrary.Common;
using System.Workflow.Activities;
using CommonLibrary.Wf.InterFace;
using ServiceApplication.Service.WorkFlow;
using ServiceApplication.Service;
using OAclient.Filters;

namespace OAclient.Controllers
{

    [ExceptionFilter]
    [LoggerFilter]
    public class TmpUsrController : Controller
    {
        //
        // GET: /TmpUsr/

        BasicService bs = new BasicService();
        WorkFlowService wfs = new WorkFlowService();
        FormService fs = new FormService();
        SerializeDesigner designer = new SerializeDesigner();

        //发起流程  
        public ActionResult WFStart(string wf_id, string user_id)
        {
            if (string.IsNullOrEmpty(wf_id))
            {
                return Content("不存在wf_id");
            }
            if (string.IsNullOrEmpty(user_id))
            {
                return Content("不存在user_id");
            }
            UserEntity user = bs.QueryUser(int.Parse(user_id));
            StaffEntity se = bs.QueryStaff(user.staff_id);
            DataTable dt_deptuser = bs.QueryUserByDeptId(se.dept_id.ToString());
            DataTable dt_duty = bs.QueryAllDutys();
            //获取本部门人员列表
            string[] arr_deptuser = dt_deptuser.AsEnumerable().Select(r => r["用户名称"].ToString()).ToArray();
            string[] arr_allduty = dt_duty.AsEnumerable().Select(x => x["职位名称"].ToString()).ToArray();
            DeptEntity dept = bs.QueryDept(se.dept_id);
            DutyEntity duty = bs.QueryDuty(se.duty_id);
            WfEntity wfEntity = wfs.QueryWf(wf_id);
            FormEntity formEntity = fs.QueryFormEntity(wfEntity.form_id);

            if (formEntity == null)
            {
                return Content("不存在该表单");
            }

            XElement xml = XElement.Load(Server.MapPath("~/UploadFiles/Form/" + formEntity.location));

            ViewData["formdefi"] = designer.DeserializeXmlToWeb(xml,
                new SerializeAutoData(true)
                {
                    user_name = user.user_name,
                    user_deptname = dept.dept_name,
                    user_dutyname = duty.duty_name,
                    user_id = user.user_id.ToString(),
                    ComboSelfStaffList = arr_deptuser,
                    ComboAllDutyList = arr_allduty
                });
            ViewData["form_id"] = formEntity.form_id.ToString();
            ViewData["wftemplate_id"] = wfEntity.wftemplate_id.ToString();
            ViewData["wf_id"] = wfEntity.wf_id.ToString();

            var platelist = System.Web.HttpContext.Current.Application[MyWorkHelpr.WorkflowStaticTemplate] as Dictionary<int, StateMachineWorkflowInstance>;
            #region 获取下一步处理人类型
            WfClrxxInfo nextClr = wfs.getClrxxByFirstNext(platelist, wfEntity.wftemplate_id);

            //初始化选择树
            //ViewData["treedefi"] = ZTreeHelper.Serialize(bs.GetWebUserNodeList());

            if (nextClr != null)
            {
                if (nextClr.clzType == TaskClrType.由上一步处理人指定.ToString())
                {
                    ViewData["treevisible"] = "T";
                }
                else //特殊情况：ifelse活动的下一步处理人无法获取，所以不是 由上一步处理人指定 的情况，在createWorkflow 方法中clr传入null
                {
                    ViewData["treevisible"] = "F";
                }
            }
            #endregion

            return PartialView();
        }

        public void AddUser(String tmpuser)
        {
            bs.AddTempUser(tmpuser);
        }

        public ActionResult QueryUser(String name)
        {
            var userdt = bs.GetTempUser(name);
            var userEntity = bs.QueryUser(name);
            var staffEntity = bs.QueryStaff(userEntity.staff_id);
            var deptEntity = bs.QueryDept(staffEntity.dept_id);
            var dutyEntity = bs.QueryDuty(staffEntity.duty_id);

            Object jsonData = new
            {
                name = ""
            };

            if (userEntity != null)
            {
                jsonData = new
                {
                    name = staffEntity.staff_name,
                    dept = deptEntity.dept_name,
                    duty = dutyEntity.duty_name,
                    gender = staffEntity.staff_sex == "1" ? "男" : "女",
                    birthday = staffEntity.staff_birth,
                    height = staffEntity.staff_height,
                    race = staffEntity.staff_nation,
                    politics = staffEntity.staff_politics,
                    marrige = staffEntity.staff_marrige,
                    schooling = staffEntity.staff_study,
                    major = staffEntity.staff_major,
                    identityCard = staffEntity.staff_sfz,
                    phone = staffEntity.staff_mobile,
                    telephone = staffEntity.staff_livenum,
                    revealed = staffEntity.staff_homeaddr,
                    address = staffEntity.staff_liveaddr,
                    timeIn = staffEntity.staff_rzsj,
                    salaryTry = staffEntity.staff_sygz,
                    salaryOfficial = staffEntity.staff_zzgz,
                    sfsb = staffEntity.staff_sfsb
                };
            }

            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        public ActionResult UserList()
        {
            ViewData["users"] = bs.GetTempUsers();
            return View();
        }

    }
}
